from bottle import route, run, request,template,static_file,response
from data import *
from os import getcwd,path
import json

DATA_TYPES=['species','moves','abilities','info','trainers','saves']
data=dict([(type,load_data(type)) for type in DATA_TYPES])
data['data-types']=data#Lol. YAY RECURSION?

def ret_dict(a,b,c=False):return json.dumps(a[b.lower if c else b] if b else [i for i in a.keys()])

def is_ajax(a):return a.headers.get('X-Requested-With')=='XMLHttpRequest'

@route('/js/<filename>')
def get_js(filename):return static_file(filename,root=path.join(getcwd(),'js'),mimetype='application/javascript')

@route('/css/<filename>')
def get_css(filename):return static_file(filename,root=path.join(getcwd(),'css'))

@route('/')
@route('/data/<a_type>')
@route('/data/<a_type>/<a_id>')
def index(a_type=None,a_id=None):
	if is_ajax(request):
		response.set_header('Content-Type','text/json')
		return ret_dict(data[a_type],a_id)
	else:return template('index',type=a_type,id=a_id)

if __name__=="__main__":run(host='localhost',port=8080,debug=True,reloader=True)